package leetcode.doublepointer;

// 验证回文串
public class IsPalindrome {
    public static void main(String[] args) {
        System.out.println(isPalindrome2("abcd+ddcbaa"));

        // 字符串正则表达式返回的是布尔值
//        System.out.println("inn--123".matches("[^A-Za-z0-9]"));
    }

    // 正则
    public boolean isPalindrome1(String s) {
        String ss = s.replaceAll("[^A-Za-z0-9]", "").toLowerCase();
        String t = new StringBuffer(ss).reverse().toString();
        return ss.equals(t);
    }

    // 双指针
    public static boolean isPalindrome2(String s) {
        int p = 0;
        int q = s.length() - 1;
        String ss = s.toLowerCase();
        while (p < q) {
            // Character.isLetterOrDigit 判断字符是不是 `字符或者数字`
            while (p < q && !Character.isLetterOrDigit(ss.charAt(p))) {
                p++;
            }
            while (p < q && !Character.isLetterOrDigit(ss.charAt(q))) {
                q--;
            }
            if (ss.charAt(p) != ss.charAt(q)) {
                return false;
            }
            p++;
            q--;
        }
        return true;
    }
}
